var colors = require("colors");
var path = require("path");
var express = require("express");
var app = express();

//设置app路由访问当前路径
app.use(express.static(path.join(__dirname, '/build')));

app.get("/interface",function(req,res){
    console.log("weixin".green);
    //接口接入
    let token = "graincode";
    let signature = req.query.signature,
        timestamp = req.query.timestamp,
        echostr   = req.query.echostr,
        nonce     = req.query.nonce;
    //参数排序
    let oriArray = [nonce,timestamp,token];
    oriArray.sort();
    console.log(oriArray);
    let original = oriArray.join('');
    //参数加密---数据校验
    var jsSHA = require('jssha');
    var shaObj = new jsSHA(original, 'TEXT');
    var scyptoString = shaObj.getHash('SHA-1', 'HEX'); 
    let pathname = __dirname+"/build/";
    if(signature == scyptoString){
        //验证成功
        pathname += "wx/index.html"
    } else {
        //验证失败
        pathname += "404.html"
    }
    fs.readFile(pathname,function (err,data){
        res.end(data);
    });
});

app.get("img/404.png",function(req, res){
    res.sendfile('./build/img/404.png');
})
// 404
app.get('*', function(req, res){
    console.log(req.url,"404".red);
    res.sendfile('./build/404.html');
});

app.listen(80);

console.log("Fore web server has running...".green);
